package chapter06

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test11_Object
 * Package: chapter06
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-06-22
 *          Time: 20:35
 */

object Test11_Object {
  def main(args: Array[String]): Unit = {
    //val student = new Student11("alice",20)
    //student.printInfo()
    //使用伴生对象创建对象
    val stu = Student11.newStudent("alice",20)

    //直接赋值 因为这个是apply创建的
    val stu1 = Student11("pop",20)
  }
}

//定义类       这里使用private 将构造方法私有化了
class Student11 private (val name: String, val age: Int) {
  def printInfo() {
    //所有的访问都是基于对象的 伴生对象也是如此
    println(s"student:name = ${name},age = ${age},school = ${Student11.school}")
  }
}

//伴生对象
object Student11{
  val school:String = "南方科技大学"

  //第一个一个对象实例的创建方法
  def newStudent(name:String,age:Int):Student11 = new Student11(name,age)

  def apply(name:String,age:Int):Student11 = new Student11(name,age)
}